package com.infinitegroup.findmystuff.activities;

import java.util.List;
import java.util.Map;

import com.infinitegroup.findmystuff.R;
import com.infinitegroup.findmystuff.presenters.ViewAllPresenter;
import com.infinitegroup.findmystuff.views.ViewAllView;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class ViewAllItemsActivity extends Activity implements ViewAllView{

	private ViewAllPresenter presenter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_viewall);
		presenter = new ViewAllPresenter(this);
		
		Button filter = (Button) findViewById(R.id.viewall_filter);
		Button returnButton = (Button) findViewById(R.id.activity_viewall_return);
		filter.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {
				String category = ((EditText)findViewById(R.id.activity_viewall_category)).getText().toString();
				if (category.equals("")) category = null;
				String status = ((EditText)findViewById(R.id.activity_viewall_status)).getText().toString();
				if (status.equals("")) status = null;
				
				Boolean date = true;//constant to determine if ascending or descending 
				RadioGroup DateRadioGroup = (RadioGroup) findViewById(R.id.date_radioGroup);
				int selectedDate  = DateRadioGroup.getCheckedRadioButtonId();
				switch(selectedDate){
				case R.id.date_radio_ascending:
					//date constant for ascending
					break;
				case R.id.date_radio_descending:
					//date constant for descending
					break;
				}
			    
				presenter.handleFilterClick(category, status, date);
			}
		});
		returnButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				presenter.handleReturnToHomeClick();
			}
		});
	}
	
	@Override
	public void onResume() {
	    super.onResume();  // Always call the superclass method first
	    
	    /*
	     * TO DO:
	     * 
	     * Based on the the information received from the filter screen set the values of the
	     * category, status and date variables. If the filter was not selected, keep it null.
	     * Then information the presenter of what these values were.
	     * The presenter will get the items to display, and this activity has to finally display them.
	     */
	    
	    String category = null, status = null;
	    boolean date=false; //?
	    
	    // Get the items to display.
	 	List<Map<String, String>> itemList = presenter.getItemsToDisplay(category, status, date);
	 	
	 	String itemString = generateItemString(itemList);
	 	((TextView) findViewById(R.id.viewall_itemfilltext)).setText(itemString);
	}
	
	/**
	 * To destroy activity when back button is pressed.
	 */
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event)
	{
	    if ((keyCode == KeyEvent.KEYCODE_BACK)){
	        finish();
	    }
	    return super.onKeyDown(keyCode, event);
	}
	
	private String generateItemString(List<Map<String, String>> itemList) {
		String result = "";
		if (itemList!=null){
			for (Map<String,String> m : itemList){
				result = result + "Name: " + m.get("itemname") + " | " + m.get("status") + "\n" + "Category: " +m.get("category") + "\n" +
						" Date: " + m.get("date") + "\n";
			}
		}
		return result;
	}

	@Override
	public void generateViewAllScreen(List<Map<String, String>> itemList) {
		String itemString = generateItemString(itemList);
	 	((TextView) findViewById(R.id.viewall_itemfilltext)).setText(itemString);
	}
	

	@Override
	public void advanceToHome(String username) { 
		// we don't need this one. Stephen decide.
		
	}

	@Override
	public void advanceToFilterView() {
		Intent intent = new Intent(this, FilterActivity.class);
		startActivity(intent);		
	}

	@Override
	public void displayMessage(String message) {
		Toast toast = Toast.makeText(this, message, Toast.LENGTH_LONG);
		toast.setGravity(Gravity.CENTER, 0, 0);
		toast.show();		
	}

	@Override
	public void end() {
		finish();	
	}

}
